<script setup>
import {
  computed,
  getCurrentInstance,
  onMounted,
  reactive,
  ref,
} from 'vue';

import qrcode from 'qrcode';
import {
  showConfirmDialog,
  showNotify,
} from 'vant';
import { useRouter } from 'vue-router';

import StatusBar from '../../components/statusBar.vue';

const { proxy } = getCurrentInstance();
const router = useRouter();

// 点击进行返回
const goback = () => {
  router.go(-1);
};
const createInfo = reactive({
  companion: [],
  hospitals: [],
  service: {},
});
onMounted(async () => {
  const { data } = await proxy.$api.h5Companion();
  Object.assign(createInfo, data.data);
  // console.log(createInfo);
});

// 定义弹出层的变量
const showHositotal = ref(false);

const showHositotalcolumns = computed(() => {
  return createInfo.hospitals.map((item) => {
    return { text: item.name, value: item.id };
  });
});

// 数据
const form = reactive({});
// 选择医院
const onConfirm = (item) => {
  console.log(item);
  form.hospital_id = item.selectedOptions[0].value;
  form.hospital_name = item.selectedOptions[0].text;
  // 关闭弹出层
  showHositotal.value = false;
};
// 就诊时间
const showStartTime = ref(false);

const currentDate = ref();
// 最小日期
const minDate = ref(new Date());
const onConfirmTime = (item) => {
  const datastr = item.selectedValues.join("-");
  currentDate.value = datastr;
  form.starttime = new Date(datastr).getTime();

  showStartTime.value = false;
};

// 陪诊师
const showcomponion = ref(false);
const showcomponioncolumns = computed(() => {
  return createInfo.companion.map((item) => {
    return { text: item.name, value: item.id };
  });
});
//默认名称的显示
const componionname = ref();
const onConfirmcomponion = (item) => {
  form.companion_id = item.selectedOptions[0].value;
  componionname.value = item.selectedOptions[0].text;
  showcomponion.value = false;
};
// 表单提交按钮
const submit = async () => {
  const params = [
    "hospital_id",
    "hospital_name",
    "demand",
    "companion_id",
    "receiveAddress",
    "tel",
    "starttime",
  ];
  for (const i of params) {
    if (!form[i]) {
      showNotify({ message: "请注意填写每一项" });
      return;
    }
  }

  const { data: orderData } = await proxy.$api.createOrder(form);

  // console.log(orderData.data.wx_code);
  qrcode.toDataURL(orderData.data.wx_code).then((url) => {
    showCode.value = true;
    codeImg.value = url;
  });
};
const showCode = ref(false);
  const codeImg = ref("");
  const closeCode = () => {
    router.push('/order')
      showCode.value=false
  };

</script>
<template>
  <div class="container">
    <div class="header">
      <van-icon name="arrow-left" @click="goback" class="header-left" size="30" />
      填写服务
    </div>
    <Status-bar item="0" />
    <van-cell class="cell">
      <template #title>
        <van-image
          width="25"
          height="25"
          :src="createInfo.service.serviceImg"
        ></van-image>
        <span class="server-name">{{ createInfo.service.serviceName }}</span>
      </template>
      <template #default>
        <div class="service-text">服务内容</div>
      </template>
    </van-cell>
    <van-cell-group class="cell">
      <van-cell>
        <template #title> 就诊医院 </template>
        <template #default>
          <div @click="showHositotal = true">
            {{ form.hospital_name || "请选择就诊医院" }}
            <van-icon name="arrow" />
          </div>
        </template>
      </van-cell>
      <van-cell>
        <template #title> 就诊时间</template>
        <template #default>
          <div @click="showStartTime = true">
            {{ currentDate || "请选择就诊时间" }}

            <van-icon name="arrow" />
          </div>
        </template>
      </van-cell>

      <van-cell>
        <template #title> 陪诊师</template>
        <template #default>
          <div @click="showcomponion = true">
            {{ componionname || "请选择陪诊师" }}

            <van-icon name="arrow" />
          </div>
        </template>
      </van-cell>
      <van-cell>
        <template #title> 接送地址</template>
        <template #default>
          <van-field
            class="text"
            input-align="right"
            v-model="form.receiveAddress"
            placeholder="请填写接送地址"
          />
        </template>
      </van-cell>
      <van-cell>
        <template #title> 联系电话</template>
        <template #default>
          <van-field
            class="text"
            input-align="right"
            v-model="form.tel"
            placeholder="请填写联系电话"
          />
        </template>
      </van-cell>
    </van-cell-group>

    <van-cell-group title="服务需求" class="cell">
      <van-field
        class="text"
        style="height: 100px"
        v-model="form.demand"
        placeholder="请简单描述下您要就诊的科室…"
      />
    </van-cell-group>
    <van-button type="primary" class="sumbit" @click="submit" size="large"
      >提交订单</van-button
    >
    <van-popup v-model:show="showHositotal" position="bottom" :style="{ height: '30%' }">
      <van-picker
        :columns="showHositotalcolumns"
        @confirm="onConfirm"
        @cancel="showHositotal = false"
      />
    </van-popup>

    <van-popup v-model:show="showStartTime" position="bottom" :style="{ height: '30%' }">
      <van-date-picker
        @confirm="onConfirmTime"
        @cancel="showStartTime = false"
        title="选择日期"
        :min-date="minDate"
        :max-date="maxDate"
      />
    </van-popup>
    <van-popup v-model:show="showcomponion" position="bottom" :style="{ height: '30%' }">
      <van-picker
        :columns="showcomponioncolumns"
        @confirm="onConfirmcomponion"
        @cancel="showcomponion = false"
      />
    </van-popup>
    <!-- 支付二维码弹窗 -->
    <van-dialog :show-confirm-button="false " v-model:show="showCode"  >
      <van-icon name="cross" class="close" @click="closeCode" />
      <div>微信支付</div>
      <van-image width="150" height="150" :src="codeImg"></van-image>
      <div>请使用本人微信扫描二维码</div>
    </van-dialog>
  </div>
</template>
<style scoped lang="less">
.container {
  background-color: #f0f0f0;
  height: 100vh;
}
.header {
  padding: 10px 0;
  text-align: center;
  line-height: 30px;
  font-size: 15px;
  .header-left {
    float: left;
  }
}

.cell {
  width: 95%;
  margin: 5px auto;
  background-color: #fff;
  ::v-deep(.van-field__control) {
    color: var(--van-cell-value-color);
  }
  ::v-deep(.van-cell__title) {
    display: flex;
    align-items: center;
  }
  .server-name {
    margin-left: 10px;
  }
}
.service-text {
  background: url()
    no-repeat center center;
  background-size: 20px;
}
.sumbit {
  position: absolute;
  bottom: 0;
}
::v-deep(.van-dialog__content) {
  text-align: center;
  padding: 20px;
  .close {
    position: absolute;
    left: 20px;
  }
}
</style>
